<?php
/**
 * List of Missions
 */
use Fox\Ddic\DataElement\Behavior;
use Fox\Behaviors;
use Hive\Mission;
use Hive\Ressource;
use Fox\Generator;
use Fox\Html\Button;
use Fox\Html\Header\Content;

/* @var $contact Hive\Ressource */
$contact = Xview::instance()->component('ressource', 'object');

// Customizing
$customizing = Hive\Ressource\Customizing::instance();
$customizing->trackSummaryPage(array('mod' => 'ressource','act'=>'missions'));

$rid = $contact->getId();
$header = new Content('missions','Missions');
$header->add(new Button('add','?',Msg(Hive::BUNDLE,Hive::MSG_ADD_TO_A_MISSION)))
		->color(Button::COLOR_WARNING)
		->behaviors(Behaviors::action(
				Generator::url(Mission::BUNDLE_NAME,'lookup',null,array('rid' => $contact->getId())),
				true,null,null,true));


// Save Posted Missions ?
$message = Xview::instance()->display(Generator::url(Ressource::BUNDLE_NAME,Mission::BUNDLE_NAME,Ressource::ACT_ADD));
// Removed Posted Missions ?
$message .= Xview::instance()->display(Generator::url(Ressource::BUNDLE_NAME,Mission::BUNDLE_NAME,Ressource::ACT_REMOVE));

$missionsPartner = Fox\Dao::finder(Mission\Partner::className())
						->where('role = ? AND partnerid = ?',array(Mission\Partner::ROLE_CANDIDATE,$contact->getId()))
						->orderBy('udate','DESC')
						->find();
?>
<div id="missionsContent" >
	<div class="page-header clearfix">
		<?php echo $header->render();?>
	</div>
	<div class="contentArea">
		<div class="mbs MissionsContent">
			<ul class="uiList">
				<?php
				echo $message;
				foreach($missionsPartner as $partner) {
					$mission = $partner->mission();
					$mid = $mission->getId();
					$remove = new Behaviors(Behaviors::action(
								Generator::url(Ressource::BUNDLE_NAME,
											   Mission::BUNDLE_NAME,
												null,
												array("rid" => $rid,
												"remove" => true,"missionid" => $mission->getId())),
								false,null,null,false,'POST',null,
								'#mid_'.$mid,null,true
								));
					$classRemove = $remove->serialize('','');
					$imageSrc = $mission->company()->logo();
					if(empty($imageSrc)) {
						$imageSrc = _URL_."/images/companies/void.gif";
					}
					// Get Partner information
					$statusLabel = $partner->step()->label();
					$date = Fox\Utilities\Dates::dateTimeToFullString($partner->udate);
					$author = Fox\User::fullName($partner->uname);
					$reason = $partner->comments;
					if(!empty($reason)) {
						$reason = '<br>'.$reason;
					} else {
						$reason = '';
					}
					$content = Msg(Hive::BUNDLE,Hive::STATUS_X1_ON_X2_BY_X3_X4,$statusLabel,$date,$author,$reason);
				?>
				<li id="mid_<?php echo $mid;?>" class="threadRow noDraft uiListItem uiListLight uiListVerticalItemBorder">
					<table class="uiGrid" cellspacing="0" cellpadding="0">
						<tbody>
							<tr>
								<td class="threadMainCol">
									<div class="UIImageBlock clearfix pvs" >
											<img class="uiImageMini" style="float:left;margin-right: 10px;" src="<?php echo $imageSrc;?>" alt="" />
											
											<div class="content UIImageBlock_Content UIImageBlock_MED_Content">
												<div class="authorsWrapper">
												<a class="threadLink" target="_blank" href="<?php echo '?mod=missions&act=main&missionid='.$mission->getId(); ?>" rel="ignore">
													<strong class="authors"><?php echo $mission->companyName().' : '.$mission->title; ?></strong>
												</a>
													<span class="mls mutualFriends fsm fwn fcg"></span>
												</div>
												<div class="snippet fsm fwn fcg">
													<span>
													<?php echo $content; ?>
													</span>
												</div>
											</div>
									</div>
								</td>
								<td class="plm">

								</td>
								<td class="pls">
									<a class="archiveLink del <?php echo $classRemove;?> " href="#">
										<?php echo Msg(Hive::BUNDLE,Hive::MSG_DELETE);?>
									</a>
								</td>
							</tr>
						</tbody>
					</table>
				</li>
				<?php }
					if(count($missionsPartner) == 0) {
						// Display No Assigned Mission Message
				?>
						<p class="prettyprint">
							<?php echo Msg(Hive::BUNDLE,Hive::MSG_NO_ASSIGNED_TO_ANY_MISSION,$contact->name());?>						
						</p>
				<?php }
				?>
			</ul>
		</div>
	</div>
</div>